// 导入基础库
import { connect, history } from 'umi';
// 导入配置参数
import df from '../../../config/dataFormat';
// 导入处理函数
import com from '../../utils/common';
import xl from '../../utils/xlsx'
import moment from 'moment';
// 导入UI组件
import { PageContainer } from '@ant-design/pro-layout';
import { Table, Button, Modal,Alert  } from 'antd';
import QF from './query_form';
// 导入LESS样式
import styles from './index.less';

const { confirm } = Modal;

const PageView = (props) => {
  // 初始化参数------------------------------------------------
  const { ui, table: tb, socketio: io, dispatch: dp } = props;

  var cols = [...df.cols];

  // 表格参数
  const table_props = {
    bordered: true,
    scroll: { x: df.width, y: 600 },
    size: 'small',
    loading: ui.loading,
    rowSelection: {
      onChange: (id, row) => {
        dp({ type: 'socketio/save', payload: { rowSelection_id: id, rowSelection_rows: row } })
      },
      selectedRowKeys: io.rowSelection_id
    },
    pagination: {
      show: true,
      showSizeChanger: false,
      current: io.skip,
      pageSize: df.pageSize,
      showTotal: (total, range) =>
        `共：${total} 条记录，当前显示：${range[0]}-${range[1]} 条记录`,
      total: io.profile_count,
      onChange: (e) => {
        dp({ type: 'socketio/save', payload: { skip: e } });
        dp({ type: 'socketio/get_profile_list' });
      },
    },
    columns: cols,
    dataSource: io.profile_list,
    onRow: (record) => {
      return {
        onContextMenu: (e) => {
          e.preventDefault();
        },
        onDoubleClick: (e) => {
          e.preventDefault();
          history.push(`/profile/view/${record._id}`);
        },
      };
    },
  };

  // 渲染UI组件------------------------------------------------
  return (
    <PageContainer
      content={
        <div style={{ height: 50, position: "relative" }}>
          <QF
            def={io.profile_query}
            reset = {()=>{
              dp({type:'socketio/save',payload:{profile_query:{}}})
            }}
            onFinish={(e) => {
              e['录入时间'] = e['录入时间']?moment(e['录入时间']).format('YYYY-MM-DD'):undefined
              e['离玉时间'] = e['离玉时间']?moment(e['离玉时间']).format('YYYY-MM-DD'):undefined
              e['来玉时间'] = e['来玉时间']?moment(e['来玉时间']).format('YYYY-MM-DD'):undefined

              dp({
                type: 'socketio/save',
                payload: { profile_query: e, skip: 1 },
              });
              dp({ type: 'socketio/get_profile_list' });
            }}
          />
        </div>
      }
      extra={[
        // <Button key="3" onClick={() => com.print()}>打印</Button>,
        <Button key="2" disabled={io.rowSelection_id.length === 0 ? true : false} onClick={() => {
          var repeat_data = xl.format_list_all(io)
          xl.export_xlsx(repeat_data, `${com.get_format_date('yyyy_MM_dd_hh_mm')}(导出).xlsx`)
          dp({ type: 'socketio/save', payload: { rowSelection_id: [], rowSelection_rows: [] } })
        }}>导出选中档案</Button>
        ,
        <Button key="1" disabled={io.rowSelection_id.length === 0 ? true : false} onClick={() => {
          confirm({
            title: '询问?',
            content: '是否要删除选中的档案?',
            onOk() {
              let ids = io.rowSelection_rows.map(row => row._id)
              dp({ type: 'socketio/emit', payload: { type: "remove_file", data: ids } })
              dp({ type: 'socketio/get_profile_list' })
              dp({ type: 'socketio/save', payload: { rowSelection_id: [], rowSelection_rows: [] } })
            }
          });
        }}>删除选择项</Button>
      ]}
    >
      <Alert message={`已选中：${io.rowSelection_id.length}条记录`} type="success" className={styles.alert} style={{display:io.rowSelection_id.length>0?'':'none'}} />
      <div id="print">
        <Table {...table_props} rowClassName={styles.td} tableLayout="fixed" rowClassName={(r, id) => {
          var className = ''
          switch (r.管控状态) {
            case '管控中':
              className = styles.gkz
              break;
            case '已过管控期':
              className = styles.ygk
              break;

            case '待核查':
              className = styles.dhc
              break;
            default:
              break;
          }
          return className

        }} />
      </div>
    </PageContainer>
  );
};

export default connect(({ ui, table, socketio }) => ({ ui, table, socketio }))(
  PageView,
);
